package com.atguigu2.preparedstatement;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import com.atguigu3.bean.Customer;
import com.atguigu3.util.JDBCUtils;

public class CustomerForQuery {
	//对customer表的查询操作
	@Test
	public void testQuery()
	{
		//执行，并返回结果集
		ResultSet resultSet = null;
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = JDBCUtils.getConnection();
			String sql = "select id,name,email,birth from customers where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, 1);
			
			resultSet = ps.executeQuery();
			//处理结果集
			if(resultSet.next())//next()：判断结果集的下一条是否有数据，如果有数据返回true,并指针下移，如果返回false,则不会下移
			{
				//获取当前这条数据的各个字段域
				int id = resultSet.getInt(1);
				String name = resultSet.getString(2);
				String email = resultSet.getString(3);
				Date date = resultSet.getDate(4);
				Customer customer = new Customer(id,name,email,date);
				System.out.println(customer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.closeResource(conn, ps, resultSet);
		}
	}

}
